package services;

import tw.edu.tku.im.maze.DeliveryListenerI;
import tw.edu.tku.im.maze.LoginRep;
import tw.edu.tku.im.maze.RegisterDeliverer;
import tw.edu.tku.im.maze.RegisterRep;
import tw.edu.tku.im.maze.RegisterReq;
import tw.edu.tku.im.maze.RepPacket;
import tw.edu.tku.im.maze.TimeOutRep;

import com.example.maze.MyLog;
import com.example.maze.R;

import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.EditText;

public class RegisterService extends IntentService
                             implements DeliveryListenerI{

   //[ ---------- Constant ----------
   public static final int REGISTER_REP_STATE_SUCCESS= 0,
                           REGISTER_REP_STATE_ERROR_ID= 1,
                           REGISTER_REP_STATE_ERROR_ID_FORMAT= 2,
                           REGISTER_REP_STATE_ERROR_NAME_FORMAT= 3,
                           REGISTER_REP_STATE_ERROR_PWD_FORMAT= 4,
                           CONNECTION_TIMEOUT                 =-1;
   public static final String BROADCAST_ACTION="REGISTER_REP",
                              STATUS="status",
                              PACKET="packet";
   //] ---------- Constant ---------- 
   
   public RegisterService() {
      super("RegisterService");
   }
   public RegisterService(String name) {
      super(name);
      // TODO Auto-generated constructor stub
   }

   @Override
   protected void onHandleIntent(Intent intent) {
      String id  = intent.getStringExtra("id");
      String name= intent.getStringExtra("name");
      String pwd = intent.getStringExtra("pwd");
      
      try {
         RegisterDeliverer mDeliverer= new RegisterDeliverer();
         mDeliverer.deliver(new RegisterReq(id, name, pwd), this);
      } catch(Exception e) {}
   }
   
   @Override
   public void processReply(RepPacket rep) {
      Intent localIntent= new Intent(BROADCAST_ACTION); 
      
      if(rep instanceof TimeOutRep) {
         localIntent.putExtra(STATUS, CONNECTION_TIMEOUT);  
      } else {
         RegisterRep mRegRep= (RegisterRep)rep;      
         switch(mRegRep.getState()) {
         case REGISTER_REP_STATE_SUCCESS: 
            localIntent.putExtra(STATUS, REGISTER_REP_STATE_SUCCESS);
            break;
         case REGISTER_REP_STATE_ERROR_ID: 
            localIntent.putExtra(STATUS, REGISTER_REP_STATE_ERROR_ID);
            break;
         case REGISTER_REP_STATE_ERROR_ID_FORMAT:             
            localIntent.putExtra(STATUS, REGISTER_REP_STATE_ERROR_ID_FORMAT);
            break;
         case REGISTER_REP_STATE_ERROR_NAME_FORMAT:             
            localIntent.putExtra(STATUS, REGISTER_REP_STATE_ERROR_NAME_FORMAT);
            break;   
         case REGISTER_REP_STATE_ERROR_PWD_FORMAT:             
            localIntent.putExtra(STATUS, REGISTER_REP_STATE_ERROR_PWD_FORMAT);
            break;     
         default: 
            //[ 可能發生未在掌控之內的不明例外
            //localIntent.putExtra(STATUS, CONNECTION_TIMEOUT);
         }
      }      
      LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
   }
}
